package rensoft.portfolio.client.lib.view;

import static com.google.gwt.query.client.GQuery.$;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.query.client.css.CSS;
import com.google.gwt.query.client.css.Length;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;

import rensoft.gwt.client.view.View;



public class EditableHTML extends View<FlowPanel> {
  private final HTML html = new HTML();

  public EditableHTML() {
    initWidget(new FlowPanel());
    widget().add(html);
    if (!GWT.isProdMode()) initializeEditMode();
  }

  public void setHTML(String value) {
    html.setHTML(value);
  }

  private void initializeEditMode() {
    Button editButton = new Button("edit");
    widget().add(editButton);
    $(asWidget()).css(CSS.POSITION.with(Position.RELATIVE));
    $(editButton).css(CSS.POSITION.with(Position.ABSOLUTE));
    $(editButton).css(CSS.RIGHT.with(Length.px(0)));
    $(editButton).css(CSS.TOP.with(Length.px(0)));
    editButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        onEditPressed();
      }
    });
  }

  private void onEditPressed() {
    new CKEditor(html).show();
  }
}
